package com.jnevision.util;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Year;
import java.time.YearMonth;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoField;
import java.util.Date;

/**
 * 
 * @author Gavin
 * @date 2018/05/30
 */
public class DateUtil {
    private static DateTimeFormatter normalFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    private static DateTimeFormatter smipleFormatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
    private static ZoneId zoneId = ZoneId.systemDefault();

    public static String getNowStart() {
        LocalDateTime time = LocalDateTime.now().withHour(0).withSecond(0).withMinute(0);

        return time.format(normalFormatter);
    }

    public static String getNowEnd() {
        LocalDateTime time = LocalDateTime.now().plusDays(1).withHour(0).withSecond(0).withMinute(0);

        return time.format(normalFormatter);
    }

    public static Long getBetweenMonthDays() {
        LocalDate now = LocalDate.now();

        LocalDate last = now.minusMonths(1);

        long a = now.toEpochDay();

        long b = last.toEpochDay();

        return a - b;
    }

    public static String getNextDay(String time) {
        return LocalDate.parse(time, DateTimeFormatter.ISO_DATE).plusDays(1).toString();
    }

    public static String getNextMonth(String yearMonth) {
        return YearMonth.parse(yearMonth).plusMonths(1).toString();
    }

    public static String getNextYear(String time) {
        return Year.parse(time).plusYears(1).toString();
    }

    public static String getNowStartPlusDays(int n) {
        LocalDateTime time = LocalDateTime.now().withHour(0).withSecond(0).withMinute(0).plusDays(n);
        return time.format(normalFormatter);
    }

    public static String getRandomOfToday() {
        LocalDateTime plusSeconds = LocalDateTime.now().withHour(8).plusSeconds((int)Math.random() * 40000 + 1);
        return plusSeconds.format(normalFormatter);
    }

    public static String getNormalTime() {
        return LocalDateTime.now().format(normalFormatter);
    }

    public static String getNormalTime(Long timestamp) {
        Instant instant = Instant.ofEpochMilli(timestamp);
        LocalDateTime time = LocalDateTime.ofInstant(instant, zoneId);

        return time.format(normalFormatter);
    }

    /***
     * 获取下月当前时间
     * 
     * @return
     */
    public static String getNextMonthTime() {
        return LocalDateTime.now().plusMonths(1).format(normalFormatter);
    }

    public static String getNormalTime(Date date) {
        Instant instant = Instant.ofEpochMilli(date.getTime());
        LocalDateTime time = LocalDateTime.ofInstant(instant, zoneId);

        return time.format(normalFormatter);
    }

    public static String getLastWeekStart() {
        LocalDateTime time =
            LocalDateTime.now().with(ChronoField.DAY_OF_WEEK, 1).withHour(0).withSecond(0).withMinute(0);

        return time.minusWeeks(1).format(normalFormatter);
    }

    public static String getLastWeekEnd() {
        LocalDateTime time =
            LocalDateTime.now().with(ChronoField.DAY_OF_WEEK, 7).withHour(0).withSecond(0).withMinute(0);

        return time.minusWeeks(1).format(normalFormatter);
    }

    public static String getWeekStart() {
        LocalDateTime time =
            LocalDateTime.now().with(ChronoField.DAY_OF_WEEK, 1).withHour(0).withSecond(0).withMinute(0);

        return time.format(normalFormatter);
    }

    public static String getWeekEnd() {
        LocalDateTime time =
            LocalDateTime.now().with(ChronoField.DAY_OF_WEEK, 7).withHour(0).withSecond(0).withMinute(0);

        return time.format(normalFormatter);
    }

    public static String getNowDate() {
        return LocalDateTime.now().format(normalFormatter);
    }

    public static String getNowDatePlusHours(int n) {
        return LocalDateTime.now().plusHours(n).format(normalFormatter);
    }

    public static String getSimpleNowDate() {
        return LocalDateTime.now().format(smipleFormatter);
    }

    public static String getDayStart() {
        LocalDateTime time = LocalDateTime.now();

        time = time.withHour(0).withMinute(0).withSecond(0);

        return time.format(normalFormatter);
    }

    public static String getDayEnd() {
        LocalDateTime time = LocalDateTime.now();

        time = time.plusDays(1).withHour(0).withMinute(0).withSecond(0);

        return time.format(normalFormatter);
    }
}
